package com.wnhz.work.yz.fragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.ReceptionEvaluateActivity;
import com.wnhz.work.yz.constant.Constants;
import com.wnhz.work.yz.fragment.shoppingcard.FitmentFragment;
import com.wnhz.work.yz.fragment.shoppingcard.ProjectFragment;
import com.wnhz.work.yz.utils.BroadCastReceiverUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * ---购物车
 */
public class HomeFragment2 extends Fragment implements View.OnClickListener , BroadCastReceiverUtil.OnReceiveBroadcast {
    private View view;
    private TextView title1, title2;
    private View line1, line2;

    private int textColorNor, textColorCli;

    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private FitmentFragment fragment1;
    private ProjectFragment fragment2;
    private TextView shop_title1;
    private int type;
    BroadcastReceiver broadcastReceiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_fragment2, container, false);
        initData();
        initView();
        broadcastReceiver = BroadCastReceiverUtil.registerBroadcastReceiver(getContext(), new String[]{ Constants.LOGO_SUCCESS}, HomeFragment2.this);
        return view;
    }

    private void initData() {
        textColorNor = getResources().getColor(R.color.text_two);
        textColorCli = getResources().getColor(R.color.main_color);
    }

    private void initView() {
        shop_title1 = (TextView) view.findViewById(R.id.shop_title1);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        view.findViewById(R.id.rl_news).setOnClickListener(this);
        view.findViewById(R.id.shop_title1).setOnClickListener(this);
        view.findViewById(R.id.shop_title2).setOnClickListener(this);

        title1 = (TextView) view.findViewById(R.id.shop_title1);
        title2 = (TextView) view.findViewById(R.id.shop_title2);
        line1 = view.findViewById(R.id.line1);
        line2 = view.findViewById(R.id.line2);

        fragmentList = new ArrayList<>();
        fragment1 = new FitmentFragment();
        fragment2 = new ProjectFragment();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);

        fragmentManager = getFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_shopping_card, fragment1);
        transaction.add(R.id.fl_shopping_card, fragment2);
        transaction.hide(fragment2);
        transaction.commit();
        loadInitData();
        line1.setVisibility(View.VISIBLE);
    }

    public void loadInitData(){
        if (MyApplication.getInstance().getUserBean() != null && MyApplication.getInstance().getUserBean().getIs_c().equals("1") || MyApplication.getInstance().getUserBean() == null) {
            view.findViewById(R.id.shop_title2).setVisibility(View.GONE);
            line1.setVisibility(View.GONE);
            shop_title1.setText("购物车");
        } else {
            view.findViewById(R.id.shop_title2).setVisibility(View.VISIBLE);
            line1.setVisibility(View.VISIBLE);
            shop_title1.setText("家居馆");
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (view != null) {
            if (MyApplication.getInstance().getUserBean() != null && MyApplication.getInstance().getUserBean().getIs_c().equals("1")) {
                view.findViewById(R.id.shop_title2).setVisibility(View.GONE);
                line1.setVisibility(View.GONE);
                shop_title1.setText("购物车");
            } else {
                view.findViewById(R.id.shop_title2).setVisibility(View.VISIBLE);
                shop_title1.setText("家居馆");
            }
        }
        Log.e("----", "onHiddenChanged");
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onResume() {
        if (view != null) {
            if (MyApplication.getInstance().getUserBean() != null && MyApplication.getInstance().getUserBean().getIs_c().equals("1")) {
                view.findViewById(R.id.shop_title2).setVisibility(View.GONE);
                shop_title1.setText("购物车");
                line1.setVisibility(View.GONE);
            } else {
                view.findViewById(R.id.shop_title2).setVisibility(View.VISIBLE);
                shop_title1.setText("家居馆");
            }
        }
        Log.e("----", "onResume");
        super.onResume();
    }

    private void hideFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == index) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }
        }
        transaction.commit();
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //消息
            case R.id.rl_news:
                intent = new Intent(getActivity(), ReceptionEvaluateActivity.class);
                startActivity(intent);
                break;

            //标题1
            case R.id.shop_title1:
                if (MyApplication.getInstance().getUserBean() != null) {
                    type = 0;
                    hideFragment(0);
                    title1.setTextColor(textColorCli);
                    title2.setTextColor(textColorNor);
                    line1.setVisibility(View.VISIBLE);
                    line2.setVisibility(View.INVISIBLE);

                    fragment1.upload();
                    fragment1.onResume();
                }
                break;

            //标题2
            case R.id.shop_title2:
                if (MyApplication.getInstance().getUserBean() != null) {
                    type = 1;
                    hideFragment(1);
                    title1.setTextColor(textColorNor);
                    title2.setTextColor(textColorCli);
                    line1.setVisibility(View.INVISIBLE);
                    line2.setVisibility(View.VISIBLE);
                    fragment2.upload();
                    fragment2.onResume();
                }
                break;

            default:
                break;
        }
    }

    public void refresh() {
        fragment1.upload();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Constants.LOGO_SUCCESS)) {
            loadInitData();
        }
    }
}
